package com.nervenets.web.controller.handler.user.order;

import com.nervenets.web.controller.Global;
import com.nervenets.web.controller.handler.Handler;
import com.nervenets.web.controller.handler.common.UserPagingParams;
import com.nervenets.web.enumeration.MovieReleaseStatus;
import com.nervenets.web.enumeration.OrderStatus;
import com.nervenets.web.enumeration.OrderType;
import com.nervenets.web.hibernate.domain.user.Order;
import com.nervenets.web.redis.domain.movie.MovieStored;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class OrdersHandler extends Handler<UserPagingParams> {
    @Override
    public String handle(UserPagingParams params, HttpServletRequest request) throws Exception {
        String hql = "from Order where userId = ? and orderType = ? and status > ? ";
        List<Object> objects = new ArrayList<>();
        objects.add(params.getUserId());
        objects.add(OrderType.movie);
        objects.add(OrderStatus.init);
        hql += "order by id desc ";

        final List<Order> orders = userDao.getAllObjectsByHql(hql, objects, params.getStart(), params.getLimit());
        List<Model> models = new ArrayList<>();
        for (Order order : orders) {
            MovieStored movie = globalService.getMovie(order.getUnionId());
            if (null != movie) models.add(new Model(order, movie, globalService.getMovieBuyMoney(order.getUnionId())));
        }
        return successMessage(models);
    }

    @Override
    public String result() throws Exception {
        return successMessage("项目数据");
    }

    @Data
    class Model {
        private int createTime;
        private long orderId;
        private String orderNumber;
        private OrderStatus orderStatus;
        private int number;
        private long priceOfUnit;
        private boolean offlinePay;
        private long deposit;
        private Long profit;

        private String name;
        private String mobile;
        private String address;
        private String expressName;
        private String expressNumber;
        private int expressTime;

        private long movieId;
        private String movieTitle;
        private String movieCoverImage;
        private MovieReleaseStatus movieReleaseStatus;
        private int userLimit;
        private long totalOfMoney;
        private long salesOfMoney;

        public Model(Order order, MovieStored movie, long salesOfMoney) {
            this.createTime = order.getCreateTime();
            this.orderId = order.getId();
            this.orderNumber = Global.OrderNumber.getMBCreateNumber(order.getCreateTime(), orderId);
            this.orderStatus = order.getStatus();
            this.number = order.getExtra().getNumber();
            this.priceOfUnit = order.getExtra().getPriceOfUnit();
            this.offlinePay = order.isOfflinePay();
            this.deposit = order.getExtra().getDeposit();
            this.profit = order.getExtra().getProfit();

            this.name = order.getExtra().getName();
            this.mobile = order.getExtra().getMobile();
            this.address = order.getExtra().getAddress();
            this.expressName = order.getExtra().getExpressName();
            this.expressNumber = order.getExtra().getExpressNumber();
            this.expressTime = order.getExtra().getExpressTime();

            this.movieId = movie.getMovieId();
            this.movieTitle = movie.getTitle();
            this.movieCoverImage = movie.getCoverImage();
            this.movieReleaseStatus = movie.getReleaseStatus();
            this.userLimit = movie.getUserLimit();
            this.totalOfMoney = movie.getTotalOfMoney();
            this.salesOfMoney = salesOfMoney;
        }
    }
}
